package ec.cd.client.tracerecorder;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;

public class TracesRecorderActivity extends Activity {
	private Spinner spinnerSamplingRate;
	private int samplingRate;
	private String samplingRateString = "Fastest";
	private boolean started = false;
	private int testLength = 500;
	private int selectedCalibration = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		spinnerSamplingRate = (Spinner) findViewById(R.id.spinnerSensorsSpeed);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.sampling_rates,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerSamplingRate.setAdapter(adapter);
		spinnerSamplingRate
				.setOnItemSelectedListener(new MyOnItemSelectedListener());
		Button button = (Button) findViewById(R.id.buttonStartStop);
		button.setOnClickListener(new MyOnClickListener());
	}

	private class RecordCalibrationTask extends AsyncTask<Void, Void, Void> {
		protected void onPreExecute() {

		}

		protected Void doInBackground(Void... objs) {
			performCalibration(testLength, selectedCalibration);
			return null;
		}

		protected void onPostExecute(Void res) {
			Button button = (Button) findViewById(R.id.buttonStartStop);
			button.setEnabled(true);
		}
	}

	private class RecordNormalTask extends AsyncTask<Void, Void, Void> {
		protected void onPreExecute() {

		}

		protected Void doInBackground(Void... objs) {
			performTraceRecording(testLength);
			return null;
		}

		protected void onPostExecute(Void res) {
			Button button = (Button) findViewById(R.id.buttonStartStop);
			button.setEnabled(true);
		}
	}

	private void performCalibration(float numMessages, int index) {
		// TextView textViewStatus = (TextView)
		// findViewById(R.id.textViewStatus);
		String[] contexts = new String[] { "Sitting", "Walking", "Running" };
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(300);
		TraceRecorder traceRecorder = new TraceRecorder(getApplicationContext());
		traceRecorder.registerTraceCalibration(contexts[index], samplingRate);
		while (traceRecorder.getNumMessagesRecorded() <= numMessages * 2) {
		}
		traceRecorder.stopListening();
		((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(300);
	}

	private void performTraceRecording(float numMessages) {
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(300);
		TraceRecorder traceRecorder = new TraceRecorder(getApplicationContext());
		traceRecorder.registerNormalTrace(samplingRate);
		while (traceRecorder.getNumMessagesRecorded() <= numMessages * 2) {
		}
		traceRecorder.stopListening();
		((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(300);
	}

	private class MyOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Button button = (Button) findViewById(R.id.buttonStartStop);
			RadioButton radioButtonCalibrationSitting = (RadioButton) findViewById(R.id.radioCalibrationRunSitting);
			RadioButton radioButtonCalibrationWalking = (RadioButton) findViewById(R.id.radioCalibrationWalking);
			RadioButton radioButtonCalibrationRunning = (RadioButton) findViewById(R.id.radioCalibrationRunning);
			RadioButton radioButtonNormalRecording = (RadioButton) findViewById(R.id.radioNormalRecording);
			EditText textTestLength = (EditText) findViewById(R.id.editTextTestLength);
			testLength = Integer.parseInt(textTestLength.getText().toString());

			if (radioButtonCalibrationSitting.isChecked()) {
				selectedCalibration = 0;
				button.setEnabled(false);
				RecordCalibrationTask recordCalibrationTask = new RecordCalibrationTask();
				recordCalibrationTask.execute((Void)null);

			}
			if (radioButtonCalibrationWalking.isChecked()) {
				selectedCalibration = 1;
				button.setEnabled(false);
				RecordCalibrationTask recordCalibrationTask = new RecordCalibrationTask();
				recordCalibrationTask.execute((Void)null);
			}
			if (radioButtonCalibrationRunning.isChecked()) {
				selectedCalibration = 2;
				button.setEnabled(false);
				RecordCalibrationTask recordCalibrationTask = new RecordCalibrationTask();
				recordCalibrationTask.execute((Void)null);
			}
			if (radioButtonNormalRecording.isChecked()) {
				started = true;
				button.setEnabled(false);
				RecordNormalTask recordNormalTask = new RecordNormalTask();
				recordNormalTask.execute((Void)null);
			}

		}
	}

	private class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			samplingRate = pos;
			samplingRateString = (String) ((TextView) view).getText();
		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.

		}
	}
}
